home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-09-25 | 8.5 KB | 391 lines | [TEXT/CWIE] |
- /*
- File: MorePreferences.cp
-
- Contains:
-
- Written by: Pete Gontier (PCG)
-
- Copyright: Copyright (c) 1998 Apple Computer, Inc.
-
- Change History (most recent first):
-
- <2> 7/24/98 PCG UPP's for collection un/flattening
- <1> 6/16/98 PCG initial checkin
- */
-
-
- #define OLDROUTINELOCATIONS 0
- #define OLDROUTINENAMES 0
- #define SystemSevenOrLater 1
-
- #ifndef __SCRIPT__
- # include <Script.h>
- #endif
-
- #ifndef __COLLECTIONS__
- # include <Collections.h>
- #endif
-
- #ifndef __FOLDERS__
- # include <Folders.h>
- #endif
-
- #ifndef __ERRORS__
- # include <Errors.h>
- #endif
-
- #include "MorePreferences.h"
- #include "MoreProcesses.h"
- #include "MoreCRC.h"
-
- enum
- {
- kPrefsFileMagicCookie = 'ferp'
- };
-
- typedef struct
- {
- UInt32 crc;
- UInt32 cookies [2];
- UInt8 flatCollectionData [ ];
- }
- tPrefsFormat, *tPrefsFormatP, **tPrefsFormatH;
-
- typedef struct
- {
- short fileRefNum;
- tPrefsFormatP prefsHeader;
- }
- tCollectionFlattenState, *tCollectionFlattenStateP;
-
- typedef struct
- {
- tPrefsFormatH prefsData;
- UInt32 offset;
- }
- tCollectionUnflattenState, *tCollectionUnflattenStateP;
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////
-
- pascal OSErr NewPrefsCollection (PrefsCollection *newPrefs)
- {
- *newPrefs = PrefsCollection (NewCollection ( ));
- if (!*newPrefs) return nilHandleErr;
- return noErr;
- }
-
- pascal OSErr DisposePrefsCollection (PrefsCollection doomedPrefs)
- {
- DisposeCollection (Collection (doomedPrefs));
- return noErr;
- }
-
- ////////////////////////////////////////////////////////////////////////////////////////////////////
-
- static pascal OSErr NewFSSpec (short vRefNum, long parID, ConstStr255Param path, FSSpecPtr *fssPP)
- {
- OSErr err = noErr;
-
- if (!(*fssPP = FSSpecPtr (NewPtr (sizeof (**fssPP)))))
- err = MemError ( );
- else
- {
- err = FSMakeFSSpec (vRefNum,parID,path,*fssPP);
-
- if (err && err != fnfErr)
- {
- DisposePtr (Ptr (*fssPP));
- *fssPP = nil;
- }
- }
-
- return err;
- }
-
- static pascal OSErr NewHParamBlock (HParmBlkPtr &hpbp)
- {
- hpbp = HParmBlkPtr (NewPtrClear (sizeof (*hpbp)));
- if (!hpbp) return MemError ( );
- return noErr;
- }
-
- static pascal OSErr ScanDirectory
- (short vRefNum, long dirID, Str31 firstNameFound, OSType fdCreator, UInt16 &count)
- {
- OSErr err = noErr;
-
- HParmBlkPtr hpbp;
-
- if (!(err = NewHParamBlock (hpbp)))
- {
- hpbp->fileParam.ioFDirIndex = 1;
- hpbp->fileParam.ioVRefNum = vRefNum;
- hpbp->fileParam.ioDirID = dirID;
- hpbp->fileParam.ioNamePtr = firstNameFound;
-
- count = 0;
-
- while (!(err = PBHGetFInfoSync (hpbp)))
- {
- if (kPrefsFileType == hpbp->fileParam.ioFlFndrInfo.fdType)
- if (fdCreator == hpbp->fileParam.ioFlFndrInfo.fdCreator)
- {
- hpbp->fileParam.ioNamePtr = nil;
- ++count;
- }
-
- hpbp->fileParam.ioFDirIndex += 1;
- hpbp->fileParam.ioDirID = dirID;
- }
-
- if (err == fnfErr)
- err = noErr;
-
- DisposePtr (Ptr (hpbp));
- if (!err) err = MemError ( );
- }
-
- return err;
- }
-
- static pascal OSErr GetPrefsSpec (ConstStr255Param fileName, OSType processSignature, FSSpecPtr &fssP, UInt16 &count)
- {
- OSErr err = noErr;
- short vRefNum;
- long dirID;
-
- if (!(err = FindFolder (kOnSystemDisk,kPreferencesFolderType,kDontCreateFolder,&vRefNum,&dirID)))
- {
- Str31 firstNameFound;
-
- if (!(err = ScanDirectory (vRefNum,dirID,firstNameFound,processSignature,count)))
- {
- if (!count)
- err = NewFSSpec (vRefNum,dirID,fileName,&fssP);
- else if (count == 1)
- err = NewFSSpec (vRefNum,dirID,firstNameFound,&fssP);
- else if (fileName)
- err = NewFSSpec (vRefNum,dirID,fileName,&fssP);
- else
- err = fnfErr;
- }
- }
-
- return err;
- }
-
- pascal OSErr GetNewPrefsCollection (PrefsCollection *newPrefs, ConstStr255Param fileName)
- {
- OSErr err = noErr;
-
- ProcessInfoRec pir;
-
- if (!(err = GetSomeProcessInfo (nil,&pir)))
- {
- FSSpecPtr fssP;
- UInt16 count;
-
- err = GetPrefsSpec (fileName,pir.processSignature,fssP,count);
-
- if (!err)
- err = GetNewPrefsCollectionFromFile (newPrefs,fssP);
-
- if (fssP)
- {
- DisposePtr (Ptr (fssP));
- if (!err) err = MemError ( );
- }
- }
-
- return err;
- }
-
- pascal OSErr WritePrefsCollection (PrefsCollection pc, ConstStr255Param fileName)
- {
- OSErr err = noErr;
-
- ProcessInfoRec pir;
-
- if (!(err = GetSomeProcessInfo (nil,&pir)))
- {
- FSSpecPtr fssP;
- UInt16 count;
- Boolean createdItHere = false;
-
- err = GetPrefsSpec (fileName,pir.processSignature,fssP,count);
-
- if (err == fnfErr && !count)
- {
- err = FSpCreate (fssP,pir.processSignature,kPrefsFileType,smSystemScript);
- if (!err) createdItHere = true;
- }
-
- if (!err)
- {
- err = WritePrefsCollectionToFile (pc,fssP);
- if (err && createdItHere) (void) FSpDelete (fssP);
- }
-
- if (fssP)
- {
- DisposePtr (Ptr (fssP));
- if (!err) err = MemError ( );
- }
- }
-
- return err;
- }
-
- static pascal OSErr CollectionUnflattenProc (SInt32 size, void *data, void *refCon)
- {
- tCollectionUnflattenStateP state = tCollectionUnflattenStateP (refCon);
- BlockMove (state->offset + (**(state->prefsData)).flatCollectionData, data, size);
- state->offset += size;
- return noErr;
- }
-
- pascal OSErr GetNewPrefsCollectionFromFile (PrefsCollection *pc, const FSSpec *fssP)
- {
- OSErr err = noErr;
-
- short fileRefNum;
-
- if (!(err = FSpOpenDF (fssP,fsRdPerm,&fileRefNum)))
- {
- Size eof;
-
- if (!(err = GetEOF (fileRefNum, &eof)))
- {
- Handle crcBuffer = NewHandle (eof);
-
- if (!crcBuffer)
- err = MemError ( );
- else
- {
- HLockHi (crcBuffer);
- if (!(err = MemError ( )))
- {
- if (!(err = FSRead (fileRefNum,&eof,*crcBuffer)))
- {
- tPrefsFormatH flatPrefs = tPrefsFormatH (crcBuffer);
-
- if ((**flatPrefs).cookies [0] != kPrefsFileMagicCookie)
- err = badFileFormat;
- else if ((**flatPrefs).cookies [1] != kPrefsFileMagicCookie)
- err = badFileFormat;
- else
- {
- UInt32 crc = MoreCRC32 (0, (**flatPrefs).flatCollectionData, eof - sizeof (tPrefsFormat));
-
- if ((**flatPrefs).crc != crc)
- err = badFileFormat;
- else if (!(err = NewPrefsCollection (pc)))
- {
- HUnlock (crcBuffer);
- if (!(err = MemError ( )))
- {
- CollectionFlattenUPP upp = NewCollectionFlattenProc (CollectionUnflattenProc);
-
- if (!upp)
- err = MemError ( );
- else
- {
- tCollectionUnflattenState unflattenState = { flatPrefs, 0 };
- err = UnflattenCollection (Collection (*pc),upp,&unflattenState);
- DisposeRoutineDescriptor (upp);
- }
- }
-
- if (err) DisposePrefsCollection (*pc);
- }
- }
- }
- }
-
- DisposeHandle (crcBuffer);
- if (!err) err = MemError ( );
- }
- }
-
- OSErr err2 = FSClose (fileRefNum);
- if (!err) err = err2;
- }
-
- return err;
- }
-
- static pascal OSErr CollectionFlattenProc (SInt32 size, void *data, void *refCon)
- {
- tCollectionFlattenStateP state = tCollectionFlattenStateP (refCon);
- state->prefsHeader->crc = MoreCRC32 (state->prefsHeader->crc, data, size);
- return FSWrite (state->fileRefNum, &size, data);
- }
-
- pascal OSErr WritePrefsCollectionToFile (PrefsCollection pc, const FSSpec *fssP)
- {
- OSErr err = noErr;
-
- short fileRefNum;
-
- if (!(err = FSpOpenDF (fssP,fsWrPerm,&fileRefNum)))
- {
- if (!(err = SetEOF (fileRefNum, sizeof (tPrefsFormat))))
- if (!(err = SetFPos (fileRefNum, fsFromStart, sizeof (tPrefsFormat))))
- {
- CollectionFlattenUPP upp = NewCollectionFlattenProc (CollectionFlattenProc);
-
- if (!upp)
- err = MemError ( );
- else
- {
- tPrefsFormat header = { 0, kPrefsFileMagicCookie, kPrefsFileMagicCookie };
- tCollectionFlattenState state = { fileRefNum, &header };
-
- if (!(err = FlattenCollection (Collection (pc), upp, &state)))
- {
- if (!(err = SetFPos (fileRefNum, fsFromStart, 0)))
- {
- Size writeSize = sizeof (header);
- err = FSWrite (fileRefNum,&writeSize,&header);
- }
- }
-
- DisposeRoutineDescriptor (upp);
- }
- }
-
- OSErr err2 = FSClose (fileRefNum);
- if (!err) err = err2;
- err2 = FlushVol (nil, fssP->vRefNum);
- if (!err) err = err2;
- }
-
- return err;
- }
-
- pascal OSErr SetPreference (PrefsCollection pc, PrefsType pt, PrefsID pid, Size ps, void *pb)
- {
- if (!MoreAssert (kPrefsReservedType != pt)) return paramErr;
-
- OSErr err = RemoveCollectionItem (Collection (pc), pt, pid);
-
- if (!err || err == collectionItemNotFoundErr)
- err = AddCollectionItem (Collection (pc), pt, pid, ps, pb);
-
- return err;
- }
-
- pascal OSErr GetPreference (PrefsCollection pc, PrefsType pt, PrefsID pid, Size ps, Size *aps, void *pb)
- {
- if (!MoreAssert (kPrefsReservedType != pt)) return paramErr;
- if (aps) *aps = ps; else aps = &ps;
- return GetCollectionItem (Collection (pc), pt, pid, aps, pb);
- }
-
- pascal OSErr PrefsAreSame (PrefsCollection,PrefsCollection, Boolean *areSame)
- {
- DebugStr ("\pno comparison performed -- returning false");
- *areSame = false;
- return noErr;
- }
-